from rdkit import Chem

def load_smiles(file_path):
    """
    从文件中加载 SMILES 分子列表
    :param file_path: 文本文件路径
    :return: SMILES 字符串列表
    """
    with open(file_path, "r") as f:
        smiles_list = [line.strip() for line in f]
    return smiles_list

def calculate_uniqueness(smiles_list):
    """
    计算分子的独特性
    :param smiles_list: SMILES 字符串列表
    :return: 独特分子数量和独特性比例
    """
    # 将 SMILES 转换为标准格式并去重
    unique_smiles = set()
    for smiles in smiles_list:
        mol = Chem.MolFromSmiles(smiles)  # 转换为分子对象
        if mol:
            canonical_smiles = Chem.MolToSmiles(mol, isomericSmiles=True)  # 生成规范化 SMILES
            unique_smiles.add(canonical_smiles)

    unique_count = len(unique_smiles)
    total_count = len(smiles_list)
    uniqueness_ratio = unique_count / total_count

    return unique_count, uniqueness_ratio

if __name__ == "__main__":
    # 文件路径
    effective_molecules_file = "./molecules_3_large_effective.txt"

    # 加载 SMILES 分子
    smiles_list = load_smiles(effective_molecules_file)

    # 计算独特性
    unique_count, uniqueness_ratio = calculate_uniqueness(smiles_list)

    # 输出结果
    print(f"总分子数量: {len(smiles_list)}")
    print(f"独特分子数量: {unique_count}")
    print(f"独特性比例: {uniqueness_ratio:.2%}")
